package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;


    /**
     * 根据分类id查询规格组
     * @param cid   分类的id
     * @return   规格组集合
     */
    public List<SpecGroupDTO> querySpecGroupByCategoryId(Long cid) {
        // 根据分类id查询
        SpecGroup record = new SpecGroup();
        record.setCid(cid);
        List<SpecGroup> specGroupList = specGroupMapper.select(record);
        // 判断
        if(CollectionUtils.isEmpty(specGroupList)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        // 返回
        return BeanHelper.copyWithCollection(specGroupList, SpecGroupDTO.class);
    }

    /**
     * 查询规格参数
     * @param gid
     * @param cid
     * @param searching
     * @return
     */
    public List<SpecParamDTO> querySpecParams(Long gid, Long cid, Boolean searching) {
        // 要求规格组和商品分类至少有一个
        if(gid == null && cid ==null){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        // 查询
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setSearching(searching);
        List<SpecParam> specParamList = specParamMapper.select(record);
        // 判断
        if(CollectionUtils.isEmpty(specParamList)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        // 返回
        return BeanHelper.copyWithCollection(specParamList, SpecParamDTO.class);
    }

    //查询规格组及组内参数
    public List<SpecGroupDTO> querySpecGroupByCId(Long cid) {
        SpecGroup group = new SpecGroup();
        group.setCid(cid);
        List<SpecGroup> specGroups = specGroupMapper.select(group);
        if(CollectionUtils.isEmpty(specGroups)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        // 转成dto
        List<SpecGroupDTO> groupDTOS = BeanHelper.copyWithCollection(specGroups, SpecGroupDTO.class);
        // 处理组内的参数
        SpecParam sp = new SpecParam();
        sp.setCid(cid);
        List<SpecParam> specParams = specParamMapper.select(sp);
        if(CollectionUtils.isEmpty(specParams)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        List<SpecParamDTO> specParamDTOS = BeanHelper.copyWithCollection(specParams, SpecParamDTO.class);

        // 先转成map
        Map<Long, List<SpecParamDTO>> specParamMap = specParamDTOS.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        groupDTOS.forEach(specGroupDTO -> {
            // 把组内参数赋值给规格组
            specGroupDTO.setParams(specParamMap.get(specGroupDTO.getId()));
        });

        /*
        第三种;推荐
        // 第一步：
        Map map = new HashMap();
        map.put(规格组id，  List<SpecParamDTO>);
        // 第二步：
        for (SpecGroupDTO groupDTO : groupDTOS) {
            groupDTO.setParams(map.get(groupDTO.getId()));
        }
        */





        /*
        第二种：双重for循环
        for (SpecGroupDTO groupDTO : groupDTOS) {
            List<SpecParamDTO> parList = new ArrayList();
            for (SpecParamDTO specParam : specParamDTOS) {
                if(specParam.getGroupId().equals(groupDTO.getId())||specParam.getGroupId()==groupDTO.getId()){
                    parList.add(specParam);
                }
            }
            groupDTO.setParams(parList);
        }*/


        /*
        第一种：循环内操作数据库，不推荐
        groupDTOS.forEach(specGroupDTO -> {
            SpecParam sp = new SpecParam();
            sp.setGroupId(specGroupDTO.getId());
            List<SpecParam> select = specParamMapper.select(sp);
            specGroupDTO.setParams(BeanHelper.copyWithCollection(select,SpecParamDTO.class));
        });*/

        // 返回
        return groupDTOS;
    }
}
